using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


/// <summary>
/// Summary description for MenuHelper
/// </summary>
public partial class ControlHelper
{
    public int FillDataSetToMenu(
    Menu menu, string relationField)
    {

        int totalRecord = 0;
        DataSet dataSet =
        sqlServerProvider.GetDataSet();
        if (dataSet != null)
        {
            if (dataSet.Tables.Count == 1)
                CreateMenuItem(menu,
                dataSet.Tables[0]);
            else
                CreateMenuItem(menu,
                dataSet.Tables[0], 
                dataSet.Tables[1],
                relationField);

        }
        else
            menu.DataSource = null;
        menu.DataBind();
        return totalRecord;
    }
    void CreateMenuItem(Menu menu, DataTable dataTable)
    {
        foreach (DataRow dataRow in dataTable.Rows)
        {
            menu.Items.Add(new MenuItem(
            dataRow[0].ToString(),
            dataRow[1].ToString()));
        }
    }
    void CreateMenuItem(MenuItem menuItem, DataTable dataTable, string fieldName, string fieldValue)
    {
        DataView dataView = new DataView(dataTable);
        dataView.RowFilter = fieldName + "='" + fieldValue + "'";
        foreach (DataRow dataRow in dataView.ToTable().Rows)
        {
            menuItem.ChildItems.Add(new MenuItem(
            dataRow[1].ToString(),
            dataRow[2].ToString()));
        }
    }
    void CreateMenuItem(Menu menu,
    DataTable firstDataTable,
    DataTable secondDataTable,
    string relationField)
    {
        int i = 0;
        foreach (DataRow dataRow in firstDataTable.Rows)
        {
            menu.Items.Add(new MenuItem(
            dataRow[0].ToString(),
            dataRow[1].ToString()));
            CreateMenuItem(menu.Items[i],
            secondDataTable,
            relationField,
            dataRow[0].ToString());
            i++;
        }
    }
}
